package cn.yqx.bankshop.order.service.impl;

import cn.yqx.bankshop.order.dao.BuGoodsStoreMapper;
import cn.yqx.bankshop.order.page.PageRequest;
import cn.yqx.bankshop.order.page.PageResult;
import cn.yqx.bankshop.order.page.PageUtils;
import cn.yqx.bankshop.order.pojo.BuGoodsStore;
import cn.yqx.bankshop.order.pojo.BuMyTrack;
import cn.yqx.bankshop.order.service.GoodCollectionService;
import cn.yqx.bankshop.order.service.GoodsService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class GoodCollectionServiceImpl implements GoodCollectionService {
    @Autowired
    BuGoodsStoreMapper mapper ;
    @Autowired
    GoodsService goodsService ;
    @Override
    public PageResult getAllGoodCollection(PageRequest pageRequest, Map<String, Object> map) {
        return PageUtils.getPageResult(pageRequest, getPageInfoTrack(pageRequest,map)) ;
    }

    private PageInfo<BuGoodsStore> getPageInfoTrack(PageRequest pageRequest, Map<String ,Object> parameterMap){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<BuGoodsStore> list = mapper.getAllGoodCollection(parameterMap) ;
        log.info(JSONObject.toJSONString(list)) ;
        List<BuGoodsStore> listNew = new ArrayList<BuGoodsStore>() ;

        list.stream().forEach(item->{
            if(StringUtils.isEmpty(item.getGoodsName())){
                Map<String,Object> map =   goodsService.findById(item.getGoodsId().longValue()) ;
                log.info("map : {} " , map);
                if(map !=null){
                    item.setGoodPicture((String) map.get("goodPicture"));
                    item.setGoodsName((String)map.get("goodsName"));
                    item.setGoodsUnitPrice((Double)map.get("goodsUnitPrice"));
                    listNew.add(item) ;
                }
            }else{
                listNew.add(item) ;
            }
        });

        return new PageInfo<BuGoodsStore>(listNew) ;
    }
}
